local skel = fk.CreateSkill {
  name = "emo__neixun",
}

Fk:loadTranslationTable{
  ["emo__neixun"] = "内训",
  [":emo__neixun"] = "结束阶段，你可令一名角色将1张或3张手牌当做【无中生有】使用，然后若其手牌数等于体力值，你获得弃牌堆一张【无懈可击】。",

  ["#emo__neixun-choose"] = "内训：选择1名角色，令其将1张或3张手牌当做【无中生有】使用",
  ["#emo__neixun-choice"] = "内训：选择其须转化的张数",
  ["#emo__neixun_viewas"] = "内训",
  ["#emo__neixun-use"] = "内训：你须将 %arg 张手牌当【无中生有】使用",

  ["$emo__neixun1"] = "得陛下怜爱，恩宠不衰。",
  ["$emo__neixun2"] = "谬蒙圣恩，光授殊宠。",
}

---@param player ServerPlayer
---@param num integer
---@return integer[]?
local getDefaultCards = function (player, num)
  local hand = player:getCardIds("h")
  for i, id in ipairs(hand) do
    if i + num - 1 > #hand then break end
    if num == 1 then
      local card = Fk:cloneCard("ex_nihilo")
      card.skillName = skel.name
      card:addSubcard(id)
      if not player:prohibitUse(card) then return {id} end
    else
      for j = i + 1, #hand do
        for k = j + 1, #hand do
          local card = Fk:cloneCard("ex_nihilo")
          card.skillName = skel.name
          card:addSubcards({id, hand[j], hand[k]})
          if not player:prohibitUse(card) then return {id, hand[j], hand[k]} end
        end
      end
    end
  end
end


skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room.alive_players, function (p) return not p:isKongcheng() end)
    if #targets == 0 then return false end
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__neixun-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local choice = "1"
    if to:getHandcardNum() > 2 then
      choice = room:askToChoice(player, {
        choices = {"1", "3"}, skill_name = skel.name, prompt = "#emo__neixun-choice",
      })
    end
    local num = tonumber(choice) or 1
    local defaultCards = getDefaultCards(to, num)
    if defaultCards then
      local _, dat = room:askToUseActiveSkill(to, {
        skill_name = "#emo__neixun_viewas", prompt = "#emo__neixun-use:::" .. num,
        extra_data = {emo__neixun = num}, cancelable = false,
      })
      local card = Fk:cloneCard("ex_nihilo")
      card.skillName = skel.name
      card:addSubcards(dat and dat.cards or defaultCards)
      room:useCard{
        from = to, tos = {to}, card = card,
      }
    end
    if not player.dead and to.hp == to:getHandcardNum() then
      local ids = room:getCardsFromPileByRule("nullification", 1, "discardPile")
      if #ids > 0 then
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
  end,
})

return skel
